package com.gigi.service.manual;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gigi.entity.FavoriteShiqinEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.mapper.generated.FavoriteShiqinMapper;
import com.gigi.model.FavoriteDO;
import com.gigi.model.ListFavoriteRequest;
import com.gigi.model.TypeTagDO;
import com.gigi.service.generated.FavoriteShiqinService;
import org.springframework.stereotype.Service;

@Service
public class FavoriteManageService {

    private final FavoriteShiqinMapper favoriteShiqinMapper;
    private final FavoriteShiqinService favoriteShiqinService;

    public FavoriteManageService(FavoriteShiqinMapper favoriteShiqinMapper, FavoriteShiqinService favoriteShiqinService) {
        this.favoriteShiqinMapper = favoriteShiqinMapper;
        this.favoriteShiqinService = favoriteShiqinService;
    }

    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<FavoriteShiqinEntity> list(ListFavoriteRequest request) {
        if (request.getPageNum() < 1) {
            request.setPageNum(1);
        }
        if (request.getPageSize() < 1) {
            request.setPageSize(10);
        }
        LambdaQueryWrapper<FavoriteShiqinEntity> wrapper = new LambdaQueryWrapper<FavoriteShiqinEntity>()
                .eq(FavoriteShiqinEntity::getUserId, request.getUserId())
                .orderByDesc(FavoriteShiqinEntity::getId);
        Page<FavoriteShiqinEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        Page<FavoriteShiqinEntity> favoritePage = favoriteShiqinMapper.selectPage(page, wrapper);
        return MultiResponse.of(favoritePage.getRecords(), (int) favoritePage.getTotal());
    }

    /**
     * 保存接口
     *
     * @param request
     * @return
     */
    public Response save(FavoriteDO request) {
        if (ObjectUtil.isNull(request.getId())) {
            FavoriteShiqinEntity target = new FavoriteShiqinEntity();
            target.setUserId(request.getUserId());
            target.setName(request.getName());
            if (!favoriteShiqinService.save(target)) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }
        } else {
            FavoriteShiqinEntity target = new FavoriteShiqinEntity();
            target.setUserId(request.getUserId());
            target.setName(request.getName());
            target.setId(request.getId());
            if (!favoriteShiqinService.updateById(target)) {
                return Response.buildFailure(ErrorCodeEnum.EDIT_FAILED.getErrCode(), ErrorCodeEnum.EDIT_FAILED.getErrDesc());
            }
        }
        return Response.buildSuccess();
    }

    /**
     * 删除接口
     *
     * @param id
     * @return
     */
    public Response delete(Long id) {
        if (!favoriteShiqinService.removeById(id)) {
            return Response.buildFailure(ErrorCodeEnum.DELETE_FAILED.getErrCode(), ErrorCodeEnum.DELETE_FAILED.getErrDesc());
        }
        return Response.buildSuccess();
    }

}
